
******************************************************************************
 * USING 2002 CENSUS DEFINITIONS FOR LAND TENURE (SW)
******************************************************************************
*** Main regressions
* Read-in land use data *
******************************************************************************
use "${data}BugandaAnalysisReplication.dta", clear

* Setting scheme plots
set scheme cleanplots, perm

tabulate year, generate(y)
foreach num of numlist 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 {
local x = `num'+2000
rename y`num' y`x'
}
rename mailo_census mailobase 
la var mailobase "Mailo, 2002 census"

gen zero = 0 
la var zero "2009"

gen free = freehold_census +leasehold_census



* Generate year interactions *
quietly{
	foreach num of numlist 2001 2002 2003 2004 2005 2006 2007 2008 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 {
	gen mailo`num' = mailobase * y`num'	
	la var mailo`num' "Mailo x `num'"	
	gen free`num' = free * y`num'		
	la var free`num' "Private x `num'"
	gen maize`num' = maize*y`num'
	gen rug`num' = ruggedness*y`num'

} 
la var customary_census "Customary, 2002 census"
la var free "Private, 2002 census"

foreach x in maize banana sorghum tea ruggedness pop_2010 dist_close dist_kampa elevation mailobase free {
		bys code3: egen m`x'=mean(`x')
		loc mx1 = "`mx1' m`x'"
	}
}

loc x "maize banana sorghum tea ruggedness pop_2010 dist_close dist_kampa elevation"
loc mx "mmaize mbanana msorghum mtea mruggedness mpop_2010 mdist_close mdist_kampa melevation mmailobase mfree"
drop y2009 

xtset id year

* Regressions: individual years: FTE LPM and logit CRE at code 3 level
******************************************************************************
* These take a long time to run, so commenting out now that they are done. 
capture log close
log using "${dofiles}annualinteractions.log", replace

foreach v in 10 30 50 {
    preserve
	keep if gte`v' == 1
	
	reghdfe forestloss_han`v' zero  mailo20* free20* y20* rug* , abs(id)  cluster(code3) 
	est sto feols_`v'_mc		
	coefplot, vertical xlabel(,angle(45)) omitted drop(_cons free* y20* rug*) order(mailo2001 mailo2002 mailo2003 mailo2004 mailo2005 mailo2006 mailo2007 mailo2008 zero mailo2010 mailo2011 mailo2012 mailo2013 mailo2014 mailo2015 mailo2016 mailo2017 mailo2018 mailo2019 ) yline(0) ci(95)
	gr save "${figures}MailoCust02`v'1.gph", replace
	
	coefplot, vertical xlabel(,angle(45)) mcolor(navy) ciopts(color(navy)) omitted drop(_cons mailo* y20* rug*) order(free2001 free2002 free2003 free2004 free2005 free2006 free2007 free2008 zero free2010 free2011 free2012 free2013 free2014 free2015 free2016 free2017 free2018 free2019 ) yline(0) ci(95)
	gr save "${figures}MailoCust02`v'2.gph", replace
	gr combine "${figures}MailoCust02`v'1.gph" "${figures}MailoCust02`v'2.gph", ycommon
	gr export "${figures}MailoCustOLS`v'.eps", replace
	
	logit forestloss_han`v' zero  mailo20*  free20* y20* `x' `mx' rug*,  cluster(code3) 
	eststo fe_`v'_mc: margins, dydx(zero mailo20* free20*) predict(pr) post
	coefplot, vertical xlabel(,angle(45)) omitted keep(_cons zero mailo* y20*) order(mailo2001 mailo2002 mailo2003 mailo2004 mailo2005 mailo2006 mailo2007 	mailo2008 zero mailo2010 mailo2011 mailo2012 mailo2013 mailo2014 mailo2015 mailo2016 mailo2017 mailo2018 mailo2019 ) yline(0) ci(95)
	gr save "${figures}MailoCust02`v'1.gph", replace
	
	coefplot, vertical xlabel(,angle(45)) mcolor(navy) ciopts(color(navy)) omitted drop(_cons mailo* y20* rug*) order(free2001 free2002 free2003 free2004 free2005 free2006 free2007 free2008 zero free2010 free2011 free2012 free2013 free2014 free2015 free2016 free2017 free2018 free2019 ) yline(0) ci(95)
	gr save "${figures}MailoCust02`v'2.gph", replace
	gr combine "${figures}MailoCust02`v'1.gph" "${figures}MailoCust02`v'2.gph", ycommon
	gr export "${figures}MailoCustLogit`v'1.eps", replace
	
	restore
	

}

log close

* Does mailo predict NTL over time?
******************************************************************************
// Standardize NTL values within year
bysort year: egen sdntl = std(ntl)

// Standardize % mailo
egen sdmailo = std(mailobase)
foreach num of numlist 2001 2002 2003 2004 2005 2006 2007 2008 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 {
	gen sdmailo`num' = sdmailo * y`num'
	la var sdmailo`num' "Mailo x `num'"
}
xtset id year
label var zero "2009"
	  
* Whole country *
reghdfe sdntl zero sdmailo20* free20* rug20* y20*, abs(id) cluster(code3) 
	coefplot, vertical xlabel(,angle(45)) omitted drop(_cons y20* rug* free*) order(sdmailo2001 sdmailo2002 sdmailo2003 sdmailo2004 sdmailo2005 sdmailo2006 sdmailo2007 sdmailo2008 zero sdmailo2010 sdmailo2011 sdmailo2012 sdmailo2013 sdmailo2014 sdmailo2015 sdmailo2016 sdmailo2017 sdmailo2018 sdmailo2019 ) yline(0) ci(95) yscale(range(-.2 .1)) ylabel(-.2(.1).1)
	gr export "${figures}MailoNTL.eps", replace




* 2-year periods *
******************************************************************************
quietly {
recode year (2001 2002 2003  = 1) ( 2004 2005 = 2) ( 2006 2007  = 3) ( 2008 2009   = 4) (2010 2011 = 5) (2012 2013 = 6) (2014 2015  = 7) (2016 2017= 8) (2018 2019 = 9)  , gen(period)

bysort id period: egen binhan50 = mean(forestloss_han50)
bysort id period: egen binhan30 = mean(forestloss_han30)
bysort id period: egen binhan10 = mean(forestloss_han10)

bysort id period: egen binlt = max(forestloss_lt)
bysort id period: keep if _n == 1
tabulat period, gen(pi)
xtset id period


foreach num of numlist 1 2 3 4 5 6 7 8 9  {
gen mailo`num' = mailobase * pi`num'
la var mailo`num' "Mailo x `num'"
}
foreach num of numlist 1 2 3 4 5 6 7 8 9  {
gen free`num' = free * pi`num'
la var free`num' "Private x `num'"
}
foreach num of numlist 1 2 3 4 5 6 7 8 9  {
g maizep`num' = maize * pi`num'	
}
foreach num of numlist 1 2 3 4 5 6 7 8 9  {
g rugp`num' = rugg * pi`num'	
}
egen sdbanana = std(banana)
foreach num of numlist 1 2 3 4 5 6 7 8 9  {
g banp`num' = sdbanana * pi`num'	
}
egen sdtea = std(tea)
foreach num of numlist 1 2 3 4 5 6 7 8 9  {
g teap`num' = sdtea * pi`num'	
}
egen sdmaize = std(maize)
foreach num of numlist 1 2 3 4 5 6 7 8 9  {
g mazp`num' = sdmaize * pi`num'	
}
egen sdsorghum = std(sorghum)
foreach num of numlist 1 2 3 4 5 6 7 8 9  {
g sorp`num' = sdsorghum * pi`num'	
}
foreach num of numlist 1 2 3 4 5 6 7 8 9  {
g bugp`num' = buganda * pi`num'	
}
}
la var zero "2008-2009" 


loc x "maize banana sorghum tea ruggedness pop_2010 dist_close dist_kampa elevation"
loc mx "mmaize mbanana msorghum mtea mruggedness mpop_2010 mdist_close mdist_kampa melevation mmailobase mfree"

foreach v in 10 30 50 {
    preserve
	keep if gte`v' == 1

**********WHOLE COUNTRY ********************

	reghdfe binhan`v' zero mailo1-mailo3 mailo5-mailo9 free1-free3 free5-free9 pi1-pi3 pi5-pi9  rugp1-rugp3 rugp5-rugp9, abs(id) cluster(code3) 
	est sto col_`v'	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* free*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011 " mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .2)) ylabel(-.1(.1).2)
	gr export "${figures}Mailo02`v'full.eps", replace
	coefplot, vertical xlabel(,angle(90)) mcolor(navy) ciopts(color(navy)) omitted drop(_cons pi* rugp* mailo*) order(free1 free2 free3 zero free5 free6 free7 free8 free9  )  ///
	coeflabels(free1 = "Private x 2001-2003" free2 = "Private x 2004-2005" free3 = "Private x 2006-2007" free5 = "Private x 2010-2011" free6 = "Private x 2012-2013" free7 = "Private x 2014-2015" free8 = "Private x 2016-2017" free9 = "Private x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .2)) ylabel(-.1(.1).2)
	gr export "${figures}free02`v'full.eps", replace	
	
	logit binhan`v' zero mailo1-mailo3 mailo5-mailo9 free1-free3 free5-free9  pi1-pi3 pi5-pi9  rugp1-rugp3 rugp5-rugp9 `x' `mx', cluster(code3) 
	eststo col_`v'_log: margins, dydx(zero mailo1-mailo3 mailo5-mailo9 free1-free3 free5-free9) predict(pr) post	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* free*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .1)) ylabel(-.1(.05).1)
	gr export "${figures}Mailo02`v'fulllogit.eps", replace
	coefplot, vertical xlabel(,angle(90)) mcolor(navy) ciopts(color(navy)) omitted drop(_cons pi* maizep* rugp* mailo*) order(free1 free2 free3 zero free5 free6 free7 free8 free9  )  ///
	coeflabels(free1 = "Private x 2001-2003" free2 = "Private x 2004-2005" free3 = "Private x 2006-2007" free5 = "Private x 2010-2011" free6 = "Private x 2012-2013" free7 = "Private x 2014-2015" free8 = "Private x 2016-2017" free9 = "Private x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .1)) ylabel(-.1(.05).1)
	gr export "${figures}free02`v'fulllogit.eps", replace	
	
	
**********BUGANDA AND BORDER *****

	reghdfe binhan`v' zero mailo1-mailo3 mailo5-mailo9  pi1-pi3 pi5-pi9 rugp1-rugp3 rugp5-rugp9 if borderbuganda==1 | buganda==1, abs(id) cluster(code3) 
	est sto colb_`v'	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .20)) ylabel(-.1(.1).20)
	gr export "${figures}Mailo02`v'Border.eps", replace
	
********* CENTRAL V WEST *********

	reghdfe binhan`v' zero mailo1-mailo3 mailo5-mailo9  pi1-pi3 pi5-pi9 rugp1-rugp3 rugp5-rugp9 if region ~= "Eastern" & region ~= "Northern", abs(id) cluster(code3) 
	est sto colw_`v'	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .20)) ylabel(-.1(.1).20)
	gr export "${figures}Mailo02`v'CentWest.eps", replace
	
	
******** CENTRAL V NORTH AND EAST ******************

reghdfe binhan`v' zero mailo1-mailo3 mailo5-mailo9  pi1-pi3 pi5-pi9 rugp1-rugp3 rugp5-rugp9 if region ~= "Western", abs(id) cluster(code3) 
	est sto colne_`v'	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9 ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .20)) ylabel(-.1(.1).20)
	gr export "${figures}Mailo02`v'NorthEast.eps", replace


// Output regressions to Latex table:
esttab col_`v' col_`v'_log colb_`v' colw_`v' colne_`v' using  "${tables}resultshansen`v'.tex", replace ///
se nonotes  style(tex)  b(%12.3f) se(%12.3f)  stats(N, labels(Obs)  fmt(%12.0gc)) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers label mlabels("" "" "" "" "") ///
	keep(mailo* free* ) ///
	coeflabels(mailo1  "Mailo x 2001-2003" mailo2 "Mailo x 2004-2005" mailo3 "Mailo x 2006-2007" mailo5  "Mailo x 2010-2011" mailo6 "Mailo x 2012-2013" mailo7 "Mailo x 2014-2015" mailo8 "Mailo x 2016-2017" mailo9 "Mailo x 2018-2019" free1 "Private x 2001-2003" free2 "Private x 2004-2005" free3 "Private x 2006-2007" free5 "Private x 2010-2011" free6  "Private x 2012-2013" free7  "Private x 2014-2015" free8  "Private x 2016-2017" free9 "Private x 2018-2019") ///
	 prehead({ \begin{tabular}{lccccc}    ///
	 \hline \hline ///
	 & \multicolumn{4}{c}{Forest loss $=$ 1, GFC Data}  \\ ///
	& (1) & (2) & (3) & (4) & (5) \\ ) ///
	prefoot( Grid FE &  yes & no &  yes & yes  \\ ///
	Logit CRE & no & yes & no & no & no \\ ///
	Time FE &  yes & yes  &  yes & yes & yes \\ ///
	Controls & yes & yes & yes & yes & yes \\  ///
	Sample & All & All & Central & Central & Central \\  ///
	& & & v border & v West & v NE \\  ) ///
	  postfoot(\hline \hline \end{tabular} } \begin{tablenotes}[para,flushleft] \tiny{The sample includes all of Uganda. ///
	    Standard errors are robust to clustering ///
	  at the county-level. All estimations include year FEs, time period indicators, and interactions of ///
	  these indicators with maize productivity and with ruggedness. Column (2) uses a CRE logit rather than a linear ///
	  probability model and includes covariates at the grid level and means of covariates at the subcounty level. * p $<$.10, ** p$<$ .05, *** p$<$.01.} \end{tablenotes} )
estimates clear

	  restore
	  }

******************* SW: CONTROLLING FOR TEA AND BANANA X PERIOD *******************
* Deals with the differential importance of cash crops over time

loc x "maize banana sorghum tea ruggedness pop_2010 dist_close dist_kampa elevation"
loc mx "mmaize mbanana msorghum mtea mruggedness mpop_2010 mdist_close mdist_kampa melevation mmailobase mfree"

foreach v in 10 30 50 {
    preserve
	keep if gte`v' == 1

**********WHOLE COUNTRY ********************

	reghdfe binhan`v' zero mailo1-mailo3 mailo5-mailo9 free1-free3 free5-free9 pi1-pi3 pi5-pi9  rugp1-rugp3 rugp5-rugp9 banp1-banp3 banp5-banp9 teap1-teap3 teap5-teap9, abs(id) cluster(code3) 
	est sto col_`v'	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* free* ban* tea*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011 " mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .2)) ylabel(-.1(.1).2)
	gr export "${figures}Mailo02`v'fullcrops.eps", replace
	coefplot, vertical xlabel(,angle(90)) mcolor(navy) ciopts(color(navy)) omitted drop(_cons pi* rugp* mailo* ban* tea*) order(free1 free2 free3 zero free5 free6 free7 free8 free9  )  ///
	coeflabels(free1 = "Private x 2001-2003" free2 = "Private x 2004-2005" free3 = "Private x 2006-2007" free5 = "Private x 2010-2011" free6 = "Private x 2012-2013" free7 = "Private x 2014-2015" free8 = "Private x 2016-2017" free9 = "Private x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .2)) ylabel(-.1(.1).2)
	gr export "${figures}free02`v'fullcrops.eps", replace	
	
	logit binhan`v' zero mailo1-mailo3 mailo5-mailo9 free1-free3 free5-free9  pi1-pi3 pi5-pi9  rugp1-rugp3 rugp5-rugp9 banp1-banp3 banp5-banp9 teap1-teap3 teap5-teap9 `x' `mx', cluster(code3) 
	eststo col_`v'_log: margins, dydx(zero mailo1-mailo3 mailo5-mailo9 free1-free3 free5-free9) predict(pr) post	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* free* ban* tea*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .1)) ylabel(-.1(.05).1)
	gr export "${figures}Mailo02`v'fulllogitcrops.eps", replace
	coefplot, vertical xlabel(,angle(90)) mcolor(navy) ciopts(color(navy)) omitted drop(_cons pi* maizep* rugp* mailo* ban* tea*) order(free1 free2 free3 zero free5 free6 free7 free8 free9  )  ///
	coeflabels(free1 = "Private x 2001-2003" free2 = "Private x 2004-2005" free3 = "Private x 2006-2007" free5 = "Private x 2010-2011" free6 = "Private x 2012-2013" free7 = "Private x 2014-2015" free8 = "Private x 2016-2017" free9 = "Private x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .1)) ylabel(-.1(.05).1)
	gr export "${figures}free02`v'fulllogitcrops.eps", replace	
	
	
**********BUGANDA AND BORDER *****

	reghdfe binhan`v' zero mailo1-mailo3 mailo5-mailo9  pi1-pi3 pi5-pi9 rugp1-rugp3 rugp5-rugp9  banp1-banp3 banp5-banp9 teap1-teap3 teap5-teap9 if borderbuganda==1 | buganda==1, abs(id) cluster(code3) 
	est sto colb_`v'	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* ban* tea*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .20)) ylabel(-.1(.1).20)
	gr export "${figures}Mailo02`v'BorderCrops.eps", replace
	
********* CENTRAL V WEST *********

	reghdfe binhan`v' zero mailo1-mailo3 mailo5-mailo9  pi1-pi3 pi5-pi9 rugp1-rugp3 rugp5-rugp9  banp1-banp3 banp5-banp9 teap1-teap3 teap5-teap9 if region ~= "Eastern" & region ~= "Northern", abs(id) cluster(code3) 
	est sto colw_`v'	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* ban* tea*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .20)) ylabel(-.1(.1).20)
	gr export "${figures}Mailo02`v'CentWestCrops.eps", replace
	
	
******** CENTRAL V NORTH AND EAST ******************

reghdfe binhan`v' zero mailo1-mailo3 mailo5-mailo9  pi1-pi3 pi5-pi9 rugp1-rugp3 rugp5-rugp9 banp1-banp3 banp5-banp9 teap1-teap3 teap5-teap9 if region ~= "Western", abs(id) cluster(code3) 
	est sto colne_`v'	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* ban* tea*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9 ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .20)) ylabel(-.1(.1).20)
	gr export "${figures}Mailo02`v'NorthEastCrops.eps", replace


// Output regressions to Latex table:
esttab col_`v' col_`v'_log colb_`v' colw_`v' colne_`v' using  "${tables}resultshansen`v'crops.tex", replace ///
se nonotes  style(tex)  b(%12.3f) se(%12.3f)  stats(N, labels(Obs)  fmt(%12.0gc)) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers label mlabels("" "" "" "" "") ///
	keep(mailo* free* ) ///
	coeflabels(mailo1  "Mailo x 2001-2003" mailo2 "Mailo x 2004-2005" mailo3 "Mailo x 2006-2007" mailo5  "Mailo x 2010-2011" mailo6 "Mailo x 2012-2013" mailo7 "Mailo x 2014-2015" mailo8 "Mailo x 2016-2017" mailo9 "Mailo x 2018-2019" free1 "Private x 2001-2003" free2 "Private x 2004-2005" free3 "Private x 2006-2007" free5 "Private x 2010-2011" free6  "Private x 2012-2013" free7  "Private x 2014-2015" free8  "Private x 2016-2017" free9 "Private x 2018-2019") ///
	 prehead({ \begin{tabular}{lccccc}    ///
	 \hline \hline ///
	 & \multicolumn{5}{c}{Forest loss $=$ 1, GFC Data}  \\ ///
	& (1) & (2) & (3) & (4) & (5)  \\ ) ///
	prefoot( Grid FE &  yes &  no & yes &  yes & yes  \\ ///
	Time FE &  yes &  yes & yes  &  yes & yes \\ ///
	Controls  & yes & yes & yes & yes & yes  \\  ///
	Sample  & All& All  & Central & Central & Central \\  ///
	& & & v border & v West & v NE \\  ) ///
	  postfoot(\hline \hline \end{tabular} } \begin{tablenotes}[para,flushleft] \tiny{Standard errors are robust to clustering at the county-level. All estimations include year FEs, time period indicators, and interactions of these indicators with banana productivity, tea productivity, and ruggedness. Column (2) uses a CRE logit rather than a linear probability model and includes covariates at the grid level and means of covariates at the subcounty level. * p $<$.10, ** p$<$ .05, *** p$<$.01.} \end{tablenotes} )
estimates clear

	  restore
	  }

	  
***************************** NIGHTTIME LIGHTS *****************************
	
********* Controlling for NTL in deforestation analysis **********	
bysort id period: egen binntl = mean(ntl)
sort id period
by id: g binntl_lag = binntl[_n-1] if period==period[_n-1]+1 // using lagged values!

loc x "maize banana sorghum tea ruggedness pop_2010 dist_close dist_kampa elevation"
loc mx "mmaize mbanana msorghum mtea mruggedness mpop_2010 mdist_close mdist_kampa melevation mmailobase mfree"

preserve
keep if gte50 == 1

* Whole Country *
reghdfe binhan50 zero binntl_lag mailo2-mailo3 mailo5-mailo9 free2-free3 free5-free9 pi1-pi3 pi5-pi9  rugp1-rugp3 rugp5-rugp9, abs(id) cluster(code3) 
	est sto col_50	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* free* binntl*) order(mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011 " mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .2)) ylabel(-.1(.1).2)
	gr export "${figures}Mailo0250fullNTL.eps", replace
	
	coefplot, vertical xlabel(,angle(90)) mcolor(navy) ciopts(color(navy)) omitted drop(_cons pi* rugp* mailo* binntl*) order(free2 free3 zero free5 free6 free7 free8 free9  )  ///
	coeflabels(free2 = "Private x 2004-2005" free3 = "Private x 2006-2007" free5 = "Private x 2010-2011" free6 = "Private x 2012-2013" free7 = "Private x 2014-2015" free8 = "Private x 2016-2017" free9 = "Private x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .2)) ylabel(-.1(.1).2)
	gr export "${figures}free0250fullNTL.eps", replace	
	
		logit binhan50 zero binntl_lag mailo2-mailo3 mailo5-mailo9 free2-free3 free5-free9  pi1-pi3 pi5-pi9  rugp1-rugp3 rugp5-rugp9  `x' `mx', cluster(code3) 
	eststo col_50_log: margins, dydx(zero mailo2-mailo3 mailo5-mailo9 free2-free3 free5-free9) predict(pr) post	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* free* binntl*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .1)) ylabel(-.1(.05).1)
	gr export "${figures}Mailo0250fulllogitNTL.eps", replace
	
	coefplot, vertical xlabel(,angle(90)) mcolor(navy) ciopts(color(navy)) omitted drop(_cons pi* maizep* rugp* mailo* binntl*) order(free1 free2 free3 zero free5 free6 free7 free8 free9  )  ///
	coeflabels(free1 = "Private x 2001-2003" free2 = "Private x 2004-2005" free3 = "Private x 2006-2007" free5 = "Private x 2010-2011" free6 = "Private x 2012-2013" free7 = "Private x 2014-2015" free8 = "Private x 2016-2017" free9 = "Private x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .1)) ylabel(-.1(.05).1)
	gr export "${figures}free0250fulllogitNTL.eps", replace	
	
	
* Buganda and Border  *
reghdfe binhan50 zero binntl_lag mailo2-mailo3 mailo5-mailo9 pi1-pi3 pi5-pi9  rugp1-rugp3 rugp5-rugp9 if borderbuganda==1 | buganda==1, abs(id) cluster(code3) 
	est sto colb_50	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* binntl* mailo1) order(mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
coeflabels(mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .20)) ylabel(-.1(.1).20)
	gr export "${figures}Mailo0250BorderNTL.eps", replace	  
	  
* Buganda and West  *
	reghdfe binhan50 zero binntl_lag mailo2-mailo3 mailo5-mailo9  pi1-pi3 pi5-pi9 rugp1-rugp3 rugp5-rugp9 if region ~= "Eastern" & region ~= "Northern", abs(id) cluster(code3) 
	est sto colw_50	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* binntl* mailo1) order(mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .20)) ylabel(-.1(.1).20)
	gr export "${figures}Mailo0250CentWestNTL.eps", replace
	
	
* Buganda and North/East  *
reghdfe binhan50 zero binntl_lag mailo2-mailo3 mailo5-mailo9  pi1-pi3 pi5-pi9 rugp1-rugp3 rugp5-rugp9 if region ~= "Western", abs(id) cluster(code3) 
	est sto colne_50	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* binntl*) order(mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9 ) ///
	coeflabels(mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011" mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .20)) ylabel(-.1(.1).20)
	gr export "${figures}Mailo0250NorthEastNTL.eps", replace


// Output regressions to Latex table:
esttab col_50 col_50_log colb_50 colw_50 colne_50 using  "${tables}resultshansen50NTL.tex", replace ///
se nonotes  style(tex)  b(%12.3f) se(%12.3f)  stats(N, labels(Obs)  fmt(%12.0gc)) ///
	starlevels(* 0.10 ** 0.05 *** 0.01) nonumbers label mlabels("" "" "" "" "") ///
	keep(mailo* free* ) noomit ///
	coeflabels(mailo1  "Mailo x 2001-2003" mailo2 "Mailo x 2004-2005" mailo3 "Mailo x 2006-2007" mailo5  "Mailo x 2010-2011" mailo6 "Mailo x 2012-2013" mailo7 "Mailo x 2014-2015" mailo8 "Mailo x 2016-2017" mailo9 "Mailo x 2018-2019" free1 "Private x 2001-2003" free2 "Private x 2004-2005" free3 "Private x 2006-2007" free5 "Private x 2010-2011" free6  "Private x 2012-2013" free7  "Private x 2014-2015" free8  "Private x 2016-2017" free9 "Private x 2018-2019") ///
	 prehead({ \begin{tabular}{lccccc}    ///
	 \hline \hline ///
	 & \multicolumn{5}{c}{Forest loss $=$ 1, GFC Data}  \\ ///
	& (1) & (2) & (3) & (4) & (5)  \\ ) ///
	prefoot( Grid FE &  yes &  no & yes &  yes & yes  \\ ///
	Time FE &  yes &  yes & yes  &  yes & yes \\ ///
	Controls  & yes & yes & yes & yes & yes  \\  ///
	Sample  & All& All  & Central & Central & Central \\  ///
	& & & v border & v West & v NE \\  ) ///
	  postfoot(\hline \hline \end{tabular} } \begin{tablenotes}[para,flushleft] \tiny{The sample includes all of Uganda. Standard errors are robust to clustering at the county-level. All estimations include year FEs, time period indicators, ruggeness interacted with time period indicators, and the nighttime light luminosity index in the previous time period. Column (2) uses a CRE logit rather than a linear probability model and includes covariates at the grid level and means of covariates at the subcounty level. * p $<$.10, ** p$<$ .05, *** p$<$.01.} \end{tablenotes} )
estimates clear	 

restore


******************* SW: CONTROLLING FOR BUGANDA KINGDOM X PERIOD *******************
foreach v in 10 30 50 {
    preserve
	keep if gte`v' == 1

**********WHOLE COUNTRY ********************

	reghdfe binhan`v' zero mailo1-mailo3 mailo5-mailo9 bugp1-bugp3 bugp5-bugp9 free1-free3 free5-free9 pi1-pi3 pi5-pi9  rugp1-rugp3 rugp5-rugp9, abs(id) cluster(code3) 
	est sto col_`v'	
	coefplot, vertical xlabel(,angle(90)) omitted drop(_cons pi* rugp* free* bug*) order(mailo1 mailo2 mailo3 zero mailo5 mailo6 mailo7 mailo8 mailo9  ) ///
	coeflabels(mailo1 = "Mailo x 2001-2003 " mailo2 = "Mailo x 2004-2005" mailo3 = "Mailo x 2006-2007 " mailo5 = "Mailo x 2010-2011 " mailo6 = "Mailo x  2012-2013" mailo7 = "Mailo x 2014-2015" mailo8 = "Mailo x 2016-2017 " mailo9 = "Mailo x 2018-2019") yline(0) ci(95) yscale(range(-.1 .2)) ylabel(-.1(.1).2)
	gr export "${figures}Mailo02`v'fullBug.eps", replace
	
	coefplot, vertical xlabel(,angle(90)) mcolor(navy) ciopts(color(navy)) omitted drop(_cons pi* rugp* mailo* free*) order(bugp1 bugp2 bugp3 zero bugp5 bugp6 bugp7 bugp8 bugp9)  ///
	coeflabels(bugp1 = "Buganda x 2001-2003" bugp2 = "Buganda x 2004-2005" bugp3 = "Buganda x 2006-2007" bugp5 = "Buganda x 2010-2011" bugp6 = "Buganda x 2012-2013" bugp7 = "Buganda x 2014-2015" bugp8 = "Buganda x 2016-2017" bugp9 = "Buganda x 2018-2019")  yline(0) ci(95) yscale(range(-.1 .2)) ylabel(-.1(.1).2)
	gr export "${figures}Bug`v'fullBug.eps", replace

	restore	
}	
 
 
 
 
 